package com.lczyfz.istep.modules.openstack.api.neutron.impl;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.FloatingIps;
import com.lczyfz.istep.modules.openstack.api.neutron.model.FloatingIp;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

public class FloatingIpImpl implements FloatingIps {
	
	public RequestFactory factory = new RequestFactory("neutron");

	@Override
	public List<FloatingIp> getFloatingIps() {
		List<FloatingIp> floatingIps = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/floatingips");
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}	
//		System.out.println(body);
		floatingIps = NeutronParse.resolveFloatingIps(body);
		
		return floatingIps;
	}

	@Override
	public FloatingIp createFloatingIp(FloatingIp floatingip) {
		
		FloatingIp newFloatingIp = new FloatingIp();
		JSONObject params = NeutronParse.packageFloatingIp(floatingip);
		System.out.println(params);
		Request request = factory.getRequest();
		request.setUrl("/v2.0/floatingips");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		
		Response results = HTTP.getConnectionResult(request);
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		newFloatingIp = NeutronParse.resolveFloatingIpDetail(res);
		return newFloatingIp;
	}

	@Override
	public void deleteFloatingIpId(String FloatingIpId) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/floatingips/" + FloatingIpId);
		request.setRequestType(RequestType.DELETE);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS  != results.getResponseCode() ){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
	}

	@Override
	public FloatingIp getFloatingIpDetail(String FloatingIpId) {
		
		FloatingIp floatingIp = new FloatingIp();
		Request request = factory.getRequest();
		request.setUrl("/v2.0/floatingips/" + FloatingIpId);
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		JSONObject body = results.getResponseBody();
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		System.out.println(body);
		floatingIp = NeutronParse.resolveFloatingIpDetail(body);
		return floatingIp;
	}

	@Override
	public FloatingIp updateFloatingIp(String FloatingIpId, FloatingIp floatingIp) {
		
		FloatingIp newFloatingIp = new FloatingIp();
		JSONObject params = NeutronParse.packageFloatingIpUpdate(floatingIp);
		Request request = factory.getRequest();
		request.setUrl("/v2.0/floatingips/" + FloatingIpId);
		request.setRequestType(RequestType.PUT);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);	
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		
		newFloatingIp = NeutronParse.resolveFloatingIpDetail(res);
		return newFloatingIp;
	}
	
	public static void main(String[] args) {
		FloatingIps impl = new FloatingIpImpl();
		List<FloatingIp>  list = impl.getFloatingIps();
	
		FloatingIp ip = impl.getFloatingIpDetail(list.get(0).getId());
	
//		impl.deleteFloatingIpId(ip.getId());
		FloatingIp newIp = new FloatingIp();
//		newIp.setTenant_id(ip.getTenant_id());
//		newIp.setProject_id(ip.getProject_id());
//		newIp.setFloating_ip_address("203.0.113.150");
//		newIp.setFloating_network_id(ip.getFloating_network_id());
//		
//		FloatingIp n = impl.createFloatingIp(newIp);
		newIp.setDescription("呵呵1");
		newIp = impl.updateFloatingIp(list.get(1).getId(), newIp);
		System.out.println(newIp.toString());
	}

}
